// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
		//士兵速度
		speed: 0,
		
		//初始化血量
		init_blood:0,
		
		anm:{
					default: null,
					type: cc.Animation,
			},

		hit_audio: {
		              type: cc.AudioClip,
		              default: null,
		          },
		laugh_audio:{
				type: cc.AudioClip,
		              default: null,
		},
		bgm:{
				type: cc.AudioClip,
		              default: null,
		},          
			

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
		//获取CANVAS结点
		var canvasNode = this.node.parent;
		//获取组件
		
		//音乐
		this.bgm_ID = cc.audioEngine.playMusic(this.bgm, true);
		this.laugh_ID = cc.audioEngine.play(this.laugh_audio, false);
		cc.audioEngine.pause(this.laugh_ID);

		// 移动方向开关
		this.dicLeft = false;
		this.dicRight = false;
		this.dicUp = false;
		this.dicDown = false;
		this.state='stand';
		this.if_play='pause';
		this.laugh_ID;
		
		//初始化键盘输入监听
		cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
		cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);	
		
	},

  //   start () {
		// this.changeState('entrance');
  //   },

    update (dt) {

		    	
    	//根据当前速度更新主角的位置
    	if (this.dicLeft) {
		    this.node.x -= this.speed;
			
		} else if (this.dicRight) {
		    this.node.x += this.speed;
		};
		if (this.dicUp){
			this.node.y += this.speed;
		}else if (this.dicDown){
			this.node.y -= this.speed;
		};
		

		// 限制士兵不能走出 CANVAS范围
		if(this.node.x > 420){
			this.node.x = 420;
		
		}else if (this.node.x < -420) {
			this.node.x = -420;
		};
		
		if(this.node.y > -11.7){
			this.node.y = -11.7;
			
		}else if (this.node.y < -232) {
			this.node.y = -232;
		};
		

		
		
		
	},
	
	onDestroy () {
	        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
	        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
	    },
		
		
		
//当用户事件输入
	onKeyDown: function (event) {
		
	        switch(event.keyCode) {
				
	            case cc.macro.KEY.w:
	            	this.changeState('run');
	                this.dicUp=true;
	                
	                break;
					
				case cc.macro.KEY.s:
					this.changeState('run');
				    this.dicDown = true;
				    
				    break;	
					
				case cc.macro.KEY.a:
					this.changeState('run');
				    this.dicLeft = true;
				    this.node.scaleX = -1;
				    
				    break;
						
				case cc.macro.KEY.d:
					this.changeState('run');
				    this.dicRight = true;
				    this.node.scaleX = 1;
				    
				    break;	
				
				case cc.macro.KEY.j:
				   this.changeState('hit1');
				   this.changeAudio('hit');
				    break;
				case cc.macro.KEY.k:
				   this.changeState('hit2');
				   this.changeAudio('hit');
				    break;	
				case cc.macro.KEY.u:
				   this.changeState('laugh');
				   this.changeAudio('laugh');

				    break;	        	
				
					
	        }
	    },
	
	onKeyUp (event) {
	    // unset a flag when key released
	    switch(event.keyCode) {
	        case cc.macro.KEY.w:
	            this.dicUp=false;
	            this.changeState('stand');
	            break;
	        	
	        case cc.macro.KEY.s:
	            this.dicDown = false;
	            this.changeState('stand');
	            break;	
	        	
	        case cc.macro.KEY.a:
	            this.dicLeft = false;
	            this.changeState('stand');
	            break;
	        		
	        case cc.macro.KEY.d:
	            this.dicRight = false;
	            this.changeState('stand');
	            break;
			case cc.macro.KEY.j:
			   	
   				this.changeState('stand');
 					
			    break;	
			case cc.macro.KEY.k:
	           
   				this.changeState('stand');
 				
	            break;
	        case cc.macro.KEY.u:
				this.node.skewX=0;
				this.node.skewY=0;
	            this.changeState('stand');
				
				 this.changeAudio('pauselaugh');
				   
	            break;    

	    }
	},
	
	changeState:function(state){
		if (this.state == state){
			return;
		};
		this.state = state;
		if('hit1'== this.state){
			this.anm.stop();
			
			this.anm.play("attack");
			
		
		
		}else if ('hit2'== this.state){
			this.anm.stop();
			
			this.anm.play("uppercut");
		
		}else if('run'== this.state){
			this.anm.stop();
			
			this.anm.play("run");

		}else if ('stand'==this.state){
			this.anm.stop();
			
			this.anm.play("stand");
		}else if ('laugh'==this.state){
			this.anm.stop();
			
			this.anm.play("laugh");
		}else if('entrance'==this.state){
			this.anm.stop();
			
			this.anm.play("Entrance");
		}
		
		
	},	

	changeAudio:function(if_play){

		
		if(this.if_play==if_play){
			return;
		}
		this.if_play=if_play;
		if('hit'==this.if_play){
			cc.audioEngine.playEffect(this.hit_audio, false);

			this.scheduleOnce(function() {
                     // 这里的 this 指向 component
                      this.changeAudio('pausehit');
                      
                }, 0.5); 

		

		}else if('laugh'==this.if_play){
			cc.audioEngine.pauseMusic(this.bgm_ID);
			cc.audioEngine.resume(this.laugh_ID);

		}else if ('pauselaugh'==this.if_play){
			cc.audioEngine.resumeMusic(this.bgm_ID);
			cc.audioEngine.pause(this.laugh_ID);
		}
	},
		
});
